# -*- codeing = utf-8 -*-

from django import forms
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
from django.conf import settings
from utils import encrypt

from user import models
from analysis.forms.bootstrap import BootStrap


# 校验用户登录页面
class UserLoginForm(BootStrap, forms.Form):
    username = forms.CharField(
        label='用户名/邮箱/手机号',
        error_messages={
            'required': '用户名/邮箱/手机号不能为空'
        }
    )
    password = forms.CharField(
        label='密码',
        error_messages={
            'required': '密码不能为空'
        },
        widget=forms.PasswordInput(render_value=True)
    )

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request

    def clean_password(self):
        password = self.cleaned_data.get('password')
        return encrypt.md5(password=password)

        # password = self.request.POST.get('password')
        # username = self.request.POST.get('username')
        # user_obj = models.UserInfo.objects.filter(username = username).first()
        # if not user_obj:
        #     self.add_error('username','用户名不存在')
        #     return password
        # if encrypt.md5(password) != user_obj.password:
        #     raise ValidationError('密码输入错误')     # ValidationError报错内容是给clean_后面的字段，这里是：password
        # return password
